<?php
namespace app\api\controller;
use think\Controller;
use thiagoalessio\TesseractOCR\TesseractOCR;




class Api extends Controller
{

//后台模板主页
public function api($y)
{
echo $y;
 //接收请求的数据
 //$question=input('post.question');
 echo "<script>alert('测试成功！');</script>";
 exit;
 //判断请求数据
 if(empty($question)){
 $this->error('查询数据不能为空！');
 }
 //连接数据库开始查询
 $data=Db::name('bank1')->where('question',$question)->select();
 //模糊查询
// $data=Db::name('bank1')->where('question','like','%{$question}%')->select();
 echo json_encode($data, JSON_UNESCAPED_UNICODE);  // 不编码中文
 echo '测试成功！';
}



               
     // 文字图像识别

  public function getImgText(){
   
   try {
    
    // 支持网络url图片地址和imgData数据
   if(isset($_GET["url"])){
    // 网络url调用
    // 文件流
   $ch = curl_init ();  
   curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );  
   curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );  
   curl_setopt ( $ch, CURLOPT_URL, $_GET["url"]);  
   ob_start ();  
   curl_exec ( $ch );  
   $content = ob_get_contents ();  
   ob_end_clean (); 
   }else if(isset($_POST["imageData"])){
    // 接受图片数据---读取数据流
    $content = $_POST["imageData"];
   }
   
   
//   echo $content;
   // 文字图像识别 
   $ocr = new TesseractOCR();
   $ocr->imageData($content, strlen($content));
   
   //语言设置
   if(isset($_GET["lang"]) || isset($_POST["lang"]) ){
       
      $lang_array = isset($_GET["lang"])?$_GET["lang"]:$_POST["lang"];
      $lang = explode('+', $lang_array);
      //语言设置   
      $ocr->lang($lang);
   }else{
       
      //默认语言
      $lang = ['eng', 'chi_sim'];
      $ocr->lang($lang);
   }
   
   $data = $ocr->run(); 
   
   $data = array(
      "code" => 200,
      "result" => $data
    );
   }catch(Exception $e){
    //   错误处理
     $data = array(
      "code" => 500,
      "result" => $e->getMessage()
      );
   }
   
   return json_encode($data, JSON_UNESCAPED_UNICODE);
}


  
  

}


